package org.example.chm;/*
 * title:
 * author:
 * date:
 * description:
 * */

import org.apache.tika.Tika;
import org.apache.tika.exception.TikaException;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.parser.microsoft.chm.ChmParser;
import org.apache.tika.sax.ToXMLContentHandler;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.xml.sax.SAXException;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
//String chmFilePath = "C:\\Users\\11924\\Java\\supportSearch\\BTS3900&BTS5900 V100R019C10SPC210 Reference-CN\\性能指标参考\\BTS3900&BTS5900 V100R019C10SPC210 性能指标参考.chm";
public class ParseChange {
    private static final int CHUNK_SIZE = 1024 * 1024; // 1MB chunk size

    public static void main(String[] args) {
        String chmFilePath = "C:\\Users\\11924\\Java\\supportSearch\\BTS3900&BTS5900 V100R019C10SPC210 Reference-CN\\性能指标参考\\BTS3900&BTS5900 V100R019C10SPC210 性能指标参考.chm";

        try (FileInputStream inputStream = new FileInputStream(new File(chmFilePath))) {
            FileChannel fileChannel = inputStream.getChannel();
            long fileSize = fileChannel.size();
            long position = 0;

            while (position < fileSize) {
                long chunkSize = Math.min(CHUNK_SIZE, fileSize - position);
                ByteBuffer buffer = ByteBuffer.allocate((int) chunkSize);
                fileChannel.read(buffer, position);
                buffer.flip();

                Tika tika = new Tika();
                ChmParser chmParser = new ChmParser();
                ToXMLContentHandler handler = new ToXMLContentHandler();
                Metadata metadata = new Metadata();
                ParseContext parseContext = new ParseContext();

                try {
                    chmParser.parse(new ByteBufferBackedInputStream(buffer), handler, metadata, parseContext);

                    // 将提取的内容转换为XML格式，然后使用jsoup进行解析
                    String htmlContent = handler.toString();
                    Document doc = Jsoup.parse(htmlContent);

                    // 获取所有标题（假设标题在 h1, h2, h3 等标签中）
                    Elements titles = doc.select("h1, h2, h3");

                    for (Element title : titles) {
                        System.out.println("Title: " + title.text());
                        // 输出该标题下直到下一个标题之前的所有内容
                        Element sibling = title.nextElementSibling();
                        while (sibling != null && !(sibling.tagName().equals("h1") || sibling.tagName().equals("h2") || sibling.tagName().equals("h3"))) {
                            System.out.println(sibling.text());
                            sibling = sibling.nextElementSibling();
                        }
                        System.out.println("--------------------------");
                    }
                } catch (IOException | TikaException | SAXException e) {
                    System.out.println("Error processing chunk at position " + position + ": " + e.getMessage());
                }

                position += chunkSize;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static class ByteBufferBackedInputStream extends java.io.InputStream {
        private final ByteBuffer buffer;

        ByteBufferBackedInputStream(ByteBuffer buffer) {
            this.buffer = buffer;
        }

        @Override
        public int read() throws IOException {
            if (!buffer.hasRemaining()) {
                return -1;
            }
            return buffer.get() & 0xFF;
        }

        @Override
        public int read(byte[] bytes, int off, int len) throws IOException {
            if (!buffer.hasRemaining()) {
                return -1;
            }

            len = Math.min(len, buffer.remaining());
            buffer.get(bytes, off, len);
            return len;
        }
    }
}
